package com.film.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;





/*
 * 该工具类 负责 获取数据库连接，以及关闭 资源
 */
public class DBUtil {
	//第一种----------------------------
//	private static final String DRIVER ="com.mysql.jdbc.Driver";
//	private static final String URL="jdbc:mysql://localhost:3306/school1?useSSL=false&characterEncoding=utf8";
//	private static final String USER="root";
//	private static final String PASSWORD="123456";
	
	//第二种-------------------
//	private static final String DRIVER;
//	private static final String URL;
//	private static final String USER;
//	private static final String PASSWORD;
//	//静态代码块 保证 DButil在第一次被加载时 就会加载数据库驱动
//	static {
//		Properties ps = null;
//		try {
//			ps = new Properties();
//			InputStream ins = new FileInputStream("db.properties");
//			ps.load(ins);
//		} catch (Exception e) {
//			System.out.println("文件没找到");
//			e.printStackTrace();
//		}
//		DRIVER = ps.getProperty("jdbc.driver");
//		URL = ps.getProperty("jdbc.url");
//		USER= ps.getProperty("jdbc.user");
//		PASSWORD = ps.getProperty("jdbc.password");
//		try {
//			Class.forName(DRIVER);
//		} catch (ClassNotFoundException e) {
//			System.err.println("数据库驱动加载失败");
//			e.printStackTrace();
//		}
//		
//	
//	}
	
	//获取数据库连接
//	public static Connection getConnecion() {
//		Connection con = null;
//		try {
//			con = DriverManager.getConnection(URL,USER,PASSWORD);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return con;
//	}
	
	
	
	//第三种------------使用Druid创建连接
	private static Properties prop = null;
	private static DataSource ds = null;
	static {
		//加载配置文件
		prop = new Properties();
		try {
			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties");
			prop.load(in);
			ds = DruidDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//设计一个方法用来返回一个数据原
	public static DataSource getDataScorse() {
		return ds;
	}
	public static Connection getConnecion() {
		//获取连接
		DataSource dsc = getDataScorse();
		Connection conn = null;
		try {
			conn = dsc.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void closeAll(ResultSet res,PreparedStatement ps,Connection con) {
		if(res!=null) {
			try {
				res.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
